package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author atguigu-mqx <bean id="1003" class="com.atguigu.tingshu.user.strategy.impl.VipStrategy"></bean>
 * @ClassName VipStrategy {"1003":AlbumStrategy}
 * @description: TODO 购买vip
 * @date 2025年09月16日
 * @version: 1.0
 */
//对象存在容器中都是以map，key-value键值对的形式存储的，所以此种形式相当于1003作为key，VipStrategy相当于value存到容器中
@Service("1003")
public class VipStrategy implements ItemTypeStrategy {

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        //  购买vip；user_vip_service - 可能需要续期；
        //  获取到购买时长;vip_service_config.id
        Long vipServiceId = userPaidRecordVo.getItemIdList().get(0);
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipServiceId);
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        //  保存数据：
        UserVipService userVipService = new UserVipService();
        //  赋值：
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipService.setUserId(userPaidRecordVo.getUserId());
        Date currentTime = new Date();
        userVipService.setStartTime(currentTime);
        //  过期时间：稍后完成; 第一种情况：第一次购买；第二中情况：续期；
        //  判断当前用户是否是vip并且未过期，续期；
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        if (userInfo.getIsVip() == 1 && userInfo.getVipExpireTime().after(currentTime)) {
            //  属于续期；
            //  LocalDateTime localDateTime = new LocalDateTime(userInfo.getVipExpireTime());
            //                Date expireTime = localDateTime.plusMonths(serviceMonth).toDate();
            //                System.out.println("过期时间：" + expireTime);
            //                userVipService.setExpireTime(expireTime);
            currentTime = userInfo.getVipExpireTime();
        }
        //            else {
        //                //  第一次购买的vip的过期时间;;
        //                //  userVipService.setExpireTime(new LocalDateTime(new Date()).plusMonths(serviceMonth).toDate());
        //            }
        //  创建对象
        LocalDateTime localDateTime = new LocalDateTime(currentTime);
        //  获取过期时间
        Date expireTime = localDateTime.plusMonths(serviceMonth).toDate();
        //  设置过期时间
        userVipService.setExpireTime(expireTime);
        //  保存vip购买记录：
        userVipServiceMapper.insert(userVipService);

        //  修改用户的vip标识;
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(expireTime);
        userInfoMapper.updateById(userInfo);
    }
}
